<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Controller;
use Think\Controller;

class RequirementController extends Controller {
    
    public function index(){
        
        $status = I('get.status') == 0 ? 0:1;
        if($status===0){
            $where = " verify = 0 or user_id != 0";
        }else{
            $where =  " verify = 1 ";
        }

        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }        
        
        $limit = 8;
        $start = ($page - 1) * $limit;  
        
        $model = M("requirement"); 
        $data = $model->where($where)->order('id desc')->limit($start,$limit)->select();
        $total = $model->where($where)->count();
        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Requirement/index', array('status'=>$status)) . '?page={page}';
        
        $render = $pagination->render();
        $this->assign('data',$data);
        $this->assign('render',$render);
        $this->assign('status',$status);
        
        $this->display('requirement');
        
    }
    
    public function publish()
    {
        $default = new DefaultController();
        $default->judgePatriarch();  
        $error = '';
        if(IS_POST){
                $data = I('post.');
                if(!preg_match('/^\d*$/',$data['price'])){
                    $this->error('请填写正确的金额格式');
                    exit;
                }
                $data['order_sn'] = $default->orderSn();
                $data['user_id'] = session('id');
                $data['date'] = date('Y-m-d');
                $model = M("requirement");
                $model->data($data)->add();                
                $this->success('添加成功等待审核',U('/Home/Requirement/index'));
                exit;
//                $this->redirect('/Home/Requirement/index');
        }
        $this->assign('error',$error);
        $this->display('publish');       
    }
    
    
    
}
